package com.zx.auth.block.mapstruct;

import com.zx.auth.block.enums.AuthorizationGrantTypeEnum;
import com.zx.auth.block.enums.ClientAuthenticationMethodEnum;
import com.zx.auth.block.modle.OAuth2RegisteredClient;
import com.zx.auth.block.param.OAuth2ClientParam;
import com.zx.auth.block.vo.OAuth2RegisteredClientVO;
import org.mapstruct.AfterMapping;
import org.mapstruct.Mapper;
import org.mapstruct.MappingTarget;
import org.mapstruct.ReportingPolicy;
import org.mapstruct.factory.Mappers;

/**
 * <p>
 * description: OAuth2客户端MapStruct映射器 <br>
 * create: 2025-11-12 16:00
 * </p>
 *
 * @author
 */
@Mapper(unmappedTargetPolicy = ReportingPolicy.IGNORE)
public interface OAuth2ClientMapStructMapper {
    OAuth2ClientMapStructMapper INSTANCE = Mappers.getMapper(OAuth2ClientMapStructMapper.class);

    /**
     * 将OAuth2ClientParam转换为OAuth2RegisteredClient实体
     *
     * @param clientParam 客户端参数
     * @return OAuth2RegisteredClient实体
     */
    OAuth2RegisteredClient clientParamToClient(OAuth2ClientParam clientParam);

    /**
     * 将OAuth2RegisteredClient实体转换为OAuth2RegisteredClientVO视图对象
     *
     * @param client 客户端实体
     * @return OAuth2RegisteredClientVO视图对象
     */
    OAuth2RegisteredClientVO clientToClientVO(OAuth2RegisteredClient client);

    /**
     * 在映射完成后，设置描述性字段
     *
     * @param client   客户端实体
     * @param clientVO 客户端VO
     */
    @AfterMapping
    default void setDescriptions(OAuth2RegisteredClient client, @MappingTarget OAuth2RegisteredClientVO clientVO) {
        // 设置客户端认证方法描述
        if (client.getClientAuthenticationMethods() != null && !client.getClientAuthenticationMethods().isEmpty()) {
            String[] methods = client.getClientAuthenticationMethods().split(",");
            StringBuilder descBuilder = new StringBuilder();
            for (int i = 0; i < methods.length; i++) {
                if (i > 0) {
                    descBuilder.append(", ");
                }
                String method = methods[i].trim();
                ClientAuthenticationMethodEnum methodEnum = ClientAuthenticationMethodEnum.getByValue(method);
                if (methodEnum != null) {
                    descBuilder.append(methodEnum.getDescription());
                } else {
                    descBuilder.append(method);
                }
            }
            clientVO.setClientAuthenticationMethodsDesc(descBuilder.toString());
        }

        // 设置授权类型描述
        if (client.getAuthorizationGrantTypes() != null && !client.getAuthorizationGrantTypes().isEmpty()) {
            String[] types = client.getAuthorizationGrantTypes().split(",");
            StringBuilder descBuilder = new StringBuilder();
            for (int i = 0; i < types.length; i++) {
                if (i > 0) {
                    descBuilder.append(", ");
                }
                String type = types[i].trim();
                AuthorizationGrantTypeEnum typeEnum = AuthorizationGrantTypeEnum.getByValue(type);
                if (typeEnum != null) {
                    descBuilder.append(typeEnum.getDescription());
                } else {
                    descBuilder.append(type);
                }
            }
            clientVO.setAuthorizationGrantTypesDesc(descBuilder.toString());
        }
    }
}